package com.gxuwz.medical.dao;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.chronicdisApply.ChronicdisApply;
import com.gxuwz.medical.exception.DbException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

public class ChronicdisApplyDao extends GenericDao<ChronicdisApply> {

	@Override
	protected ChronicdisApply handle(ResultSet rs) throws SQLException {
		try{
		String idcard = rs.getString("idcard");
		String name = rs.getString("name");
		String createtime= rs.getString("createtime");
		BigDecimal applyAcount= rs.getBigDecimal("applyAcount");
		String chronicname  = rs.getString("chronicname");
		
		ChronicdisApply chronicdisApply = new ChronicdisApply(idcard,name,applyAcount,createtime,chronicname);
		return chronicdisApply;
		}catch(Exception e){
			throw new SQLException("结果集转化为实例失败!",e);
		}
	}
	
	/**
	 * 统计已经报销的金额
	 * @return
	 */
	public Double count(String idcard){
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Double c = (double) 0 ;
		try{
			conn = DbUtil.getConn();
			StringBuffer sqlBuffer = new StringBuffer("select sum(applyAcount) as total from t_chronicdis_apply where idcard=?");
			pstmt = conn.prepareStatement(sqlBuffer.toString());
			pstmt.setString(1,idcard);
			rs = pstmt.executeQuery();
			while(rs.next()){
				c = rs.getDouble("total");
//				System.out.println(c);
				return c;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return c;
	}
	
	/**
	 * 查询所有报销单信息
	 * @return
	 * @throws DbException
	 */
	public List<ChronicdisApply> ChronicdisApplyList() throws DbException{
		try{
			String sql = "select * from t_chronicdis_apply";
			Object[]params={};
			return super.queryOjects(sql, params);
		}catch(SQLException e){
			throw new DbException("查询失败");
		}
	}

	

}
